package com.zqwh.xtool.model.online;


import android.content.Context;

import com.zqwh.yj.sdk.utils.httpRequest.RetrofitSingleton;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.NetCallback;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.ProgressSubscriber;
import com.zqwh.yj.sdk.utils.httpRequest.HttpResultFunc2;
import com.zqwh.xtool.api.offline.UserResposity;
import com.zqwh.xtool.api.online.UserApi;
import com.zqwh.xtool.config.BaseConfig;
import com.zqwh.xtool.domain.UserVO;
import com.zqwh.xtool.domain.UserTemplatesVO;
import com.zqwh.xtool.greendao.db.User;
import com.zqwh.xtool.model.UserModel;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.zqwh.xtool.ToolsApp.gUser;


/**
 * Created by eason.yang on 2018/2/7.
 */

public class UserModelOnlineImpl implements UserModel {
    private UserApi userApi;
    private UserResposity userService;
    private Context context;

    public UserModelOnlineImpl(Context context) {
        this.context = context;
        this.userApi = RetrofitSingleton.getInstance(BaseConfig.BASE_URL).create(UserApi.class);
        this.userService = new UserResposity(this.context);
    }

    @Override
    public void signInByAccount(Long devId, String username, String password, NetCallback<UserVO> callback) {
        userApi.signInByAccount(devId, username, password)
                .map(new HttpResultFunc2<UserVO>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void signInByCardNo(Long devId, String cardNo, NetCallback<UserVO> callback) {
        userApi.signInByCardNo(devId, cardNo)
                .map(new HttpResultFunc2<UserVO>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void signInById(Long devId, Long userId, NetCallback<UserVO> callback) {
        userApi.signInById(devId, userId)
                .map(new HttpResultFunc2<UserVO>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void getTemplateList(NetCallback<List<UserTemplatesVO>> callback) {
        userApi.getTemplateList()
                .map(new HttpResultFunc2<List<UserTemplatesVO>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void setTemplate(Long userId, Integer templateNum, String template, NetCallback<Object> callback) {
        userApi.setTemplate("Bearer "+gUser.getToken(), userId, templateNum, template)
                .map(new HttpResultFunc2<Object>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public boolean insertMultUser(List<User> userList) {
        return userService.insertMultUser(userList);
    }

    @Override
    public List<User> queryAllUser() {
        return userService.queryAllUser();
    }
}
